import { useLazyQuery, useQuery } from "@apollo/client";
import {
  getOrganizationInfo,
  getOrganizationInfoSimple,
  getOrganizations_simple,
} from "../../graphql/organization";
import { OrganizationType } from "../../types/organization";

export const useFetchGetOrganizations = () => {
  const { loading, data } = useQuery(getOrganizations_simple, {
    variables: { page: { pageNum: 1, pageSize: 0 } },
    fetchPolicy: "network-only",
  });
  return {
    loading,
    data: data?.getOrganizationsForH5?.data as {
      id: string;
      name: string;
      address: string;
    }[],
  };
};

export const useGetOrganizationInfo = (id: string) => {
  const { loading, refetch, data } = useQuery(getOrganizationInfo, {
    variables: {
      id,
    },
    fetchPolicy: "network-only",
  });
  return {
    loading,
    refetch,
    data: data?.getOrganizationInfo.data as OrganizationType,
  };
};

export const useGetOrgInfoSimple = () => {
  const [fun, { loading }] = useLazyQuery(getOrganizationInfoSimple, {
    fetchPolicy: "network-only",
  });
  const getOrgInfoLazy = async (id: string) => {
    const res = await fun({
      variables: {
        id,
      },
    });
    return res.data?.getOrganizationInfo.data as OrganizationType;
  };

  return {
    getOrgInfoLazy,
    loading,
  };
};
